// 汇总表: 需要用三个表组装
import React, {useEffect, useState} from 'react';
import CardTable from '~/page/FineBi/common/CardTable';
import {queryValues} from '~/service/refundReport';
import {COMMON_KEY} from '../config';

const idConfig = {
    xiao: 'CentralScoring_PlateDiagnosis_PartOne',
    chu: 'CentralScoring_PlateDiagnosis_PartFive',
    gao: 'CentralScoring_PlateDiagnosis_PartSix',
};

function TableItem(props) {
    const {children, dimensionAndValues, dId} = props;
    const [loading, setLoading] = useState(false);
    const [list, setList] = useState([]);

    // 手动组装数据
    useEffect(() => {
        setLoading(true);

        const pDimensionAndValues1 = dimensionAndValues.map(item => ({
            ...item,
            dimensionId: `${idConfig.xiao}_${COMMON_KEY}`,
            dimensionName: COMMON_KEY
        }));
        const param1 = {
            dimensionAndValues: pDimensionAndValues1,
            dashBoardList: [
                {
                    dashBoardName: idConfig.xiao,
                    dashBoardId: idConfig.xiao
                }
            ]
        };
        const xiao = queryValues(param1).then(res => res.data?.[idConfig.xiao]?.[`${idConfig.xiao}_excel`]);

        const pDimensionAndValues2 = dimensionAndValues.map(item => ({
            ...item,
            dimensionId: `${idConfig.chu}_${COMMON_KEY}`,
            dimensionName: COMMON_KEY
        }));
        const param2 = {
            dimensionAndValues: pDimensionAndValues2,
            dashBoardList: [
                {
                    dashBoardName: idConfig.chu,
                    dashBoardId: idConfig.chu
                }
            ]
        };
        const chu = queryValues(param2).then(res => res.data?.[idConfig.chu]?.[`${idConfig.chu}_excel`]);

        const pDimensionAndValues3 = dimensionAndValues.map(item => ({
            ...item,
            dimensionId: `${idConfig.gao}_${COMMON_KEY}`,
            dimensionName: COMMON_KEY
        }));
        const param3 = {
            dimensionAndValues: pDimensionAndValues3,
            dashBoardList: [
                {
                    dashBoardName: idConfig.gao,
                    dashBoardId: idConfig.gao
                }
            ]
        };
        const gao = queryValues(param3).then(res => res.data?.[idConfig.gao]?.[`${idConfig.gao}_excel`]);

        Promise.all([xiao, chu, gao])
            .then(resArr => {
                const [data1, data2, data3] = resArr;
                const columnList = [];

                resArr.forEach(item => {
                    item?.columnList?.forEach(child => {
                        if (columnList.every(ele => ele.name !== child.name)) {
                            columnList.push(child);
                        }
                    });
                });

                const value = data1?.value?.map((child, index) => ({
                    ...child,
                    ...data2.value[index],
                    ...data3.value[index],
                }));

                const dataRes = {
                    unitId: '',
                    unitName: '',
                    columnList,
                    value
                };

                setList(dataRes);
            })
            .finally(() => {
                setLoading(false);
            });
    }, [dimensionAndValues]);

    return (
        <CardTable
            unitId="all"
            unitName="带班量"
            data={list}
            loading={loading}
            span={12}
            height={300}
            tableProps={{pagination: false}}
        />
    );
}

export default TableItem;

